<template>
  <div class="register-form">
    <div class="form-scrollable">
      <div class="form-content">
        <div class="form-fields">
          <div class="form-field">
            <div class="input-container">
              <span class="input-icon">
                <i class="fa-regular fa-user"></i>
              </span>
              <input type="text" v-model="form.username" :class="{ 'input-error': errors.username }" class="text-input"
                placeholder="请输入您的用户名">
            </div>
            <div v-if="errors.username" class="error-message">
              <i class="fa-solid fa-circle-exclamation"></i>
              {{ errors.username }}
            </div>
          </div>

          <div class="form-field">
            <div class="input-container">
              <span class="input-icon">
                <i class="fa-regular fa-envelope"></i>
              </span>
              <input type="email" v-model="form.email" :class="{ 'input-error': errors.email }" class="text-input"
                placeholder="请输入您的邮箱">
            </div>
            <div v-if="errors.email" class="error-message">
              <i class="fa-solid fa-circle-exclamation"></i>
              {{ errors.email }}
            </div>
          </div>

          <div class="form-field">
            <div class="input-container">
              <span class="input-icon">
                <i class="fa-solid fa-key"></i>
              </span>
              <input :type="form.showPassword ? 'text' : 'password'" v-model="form.password"
                :class="{ 'input-error': errors.password }" class="text-input" placeholder="创建密码 (最少6个字符)">
              <button type="button" @click="form.showPassword = !form.showPassword" class="toggle-password">
                <i :class="form.showPassword ? 'fa-regular fa-eye-slash' : 'fa-regular fa-eye'"></i>
              </button>
            </div>
            <div v-if="errors.password" class="error-message">
              <i class="fa-solid fa-circle-exclamation"></i>
              {{ errors.password }}
            </div>
          </div>

          <div class="form-field">
            <div class="input-container">
              <span class="input-icon">
                <i class="fa-solid fa-key"></i>
              </span>
              <input :type="form.showConfirmPassword ? 'text' : 'password'" v-model="form.confirmPassword"
                :class="{ 'input-error': errors.confirmPassword }" class="text-input" placeholder="再次输入密码">
              <button type="button" @click="form.showConfirmPassword = !form.showConfirmPassword" class="toggle-password">
                <i :class="form.showConfirmPassword ? 'fa-regular fa-eye-slash' : 'fa-regular fa-eye'"></i>
              </button>
            </div>
            <div v-if="errors.confirmPassword" class="error-message">
              <i class="fa-solid fa-circle-exclamation"></i>
              {{ errors.confirmPassword }}
            </div>
          </div>

          <div class="form-field">
            <div class="terms-container" :class="{ 'error-terms': errors.agreeTerms }">
              <input id="terms" type="checkbox" v-model="form.agreeTerms" class="terms-checkbox">
              <label for="terms" class="terms-text">
                我同意
                <a href="#" @click.prevent="emitShowTerms" class="terms-link">服务条款</a>
                和
                <a href="#" @click.prevent="emitShowPrivacyPolicy" class="terms-link">隐私政策</a>
              </label>
            </div>
            <div v-if="errors.agreeTerms" class="error-message">
              <i class="fa-solid fa-circle-exclamation"></i>
              {{ errors.agreeTerms }}
            </div>
          </div>
        </div>
      </div>
      
      <!-- 固定在底部的按钮 -->
      <div class="fixed-button-container">
        <button @click="handleSubmit" class="register-button">
          <span>注册账号</span>
          <i class="fa-solid fa-arrow-right"></i>
        </button>
      </div>
    </div>

    <div class="separator">
      <div class="separator-line"></div>
      <span class="separator-text">或者使用</span>
      <div class="separator-line"></div>
    </div>

    <div class="social-login">
      <a href="#" class="social-button">
        <i class="fa-brands fa-github"></i>
        <span>GitHub</span>
      </a>
      <a href="#" class="social-button">
        <i class="fa-brands fa-weixin"></i>
        <span>微信</span>
      </a>
      <a href="#" class="social-button">
        <i class="fa-brands fa-qq"></i>
        <span>QQ</span>
      </a>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'

// 定义表单接口
interface RegisterForm {
  username: string;
  email: string;
  password: string;
  confirmPassword: string;
  agreeTerms: boolean;
  showPassword: boolean;
  showConfirmPassword: boolean;
}

// 定义错误接口
interface RegisterFormErrors {
  username: string;
  email: string;
  password: string;
  confirmPassword: string;
  agreeTerms: string;
  [key: string]: string; // 索引签名，允许字符串索引
}

// 表单数据
const form = reactive<RegisterForm>({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  agreeTerms: false,
  showPassword: false,
  showConfirmPassword: false
})

// 错误信息
const errors = reactive<RegisterFormErrors>({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  agreeTerms: ''
})

const emits = defineEmits(['register', 'show-terms', 'show-privacy-policy'])

// 邮箱验证函数
const isValidEmail = (email: string): boolean => {
  const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  return re.test(email)
}

// 验证表单
const validateForm = () => {
  let isValid = true

  // 重置所有错误
  Object.keys(errors).forEach(key => {
    errors[key] = ''
  })

  if (!form.username) {
    errors.username = '用户名不能为空'
    isValid = false
  } else if (form.username.length < 3) {
    errors.username = '用户名长度不足'
    isValid = false
  }

  if (!form.email) {
    errors.email = '邮箱不能为空'
    isValid = false
  } else if (!isValidEmail(form.email)) {
    errors.email = '邮箱格式无效'
    isValid = false
  }

  if (!form.password) {
    errors.password = '密码不能为空'
    isValid = false
  } else if (form.password.length < 6) {
    errors.password = '密码长度不足'
    isValid = false
  } else if (!/\d/.test(form.password) || !/[a-zA-Z]/.test(form.password)) {
    errors.password = '密码格式无效'
    isValid = false
  }

  if (!form.confirmPassword) {
    errors.confirmPassword = '确认密码不能为空'
    isValid = false
  } else if (form.confirmPassword !== form.password) {
    errors.confirmPassword = '两次密码不一致'
    isValid = false
  }

  if (!form.agreeTerms) {
    errors.agreeTerms = '请同意条款'
    isValid = false
  }

  return isValid
}

// 处理提交
const handleSubmit = () => {
  if (validateForm()) {
    emits('register', { ...form })
  }
}

// 触发显示条款和隐私政策
const emitShowTerms = () => emits('show-terms')
const emitShowPrivacyPolicy = () => emits('show-privacy-policy')
</script>

<style scoped>
* {
  box-sizing: border-box;
}

.register-form {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
  padding-bottom: 70px; /* 为固定按钮留出空间 */
}

.form-scrollable {
  flex: 1;
  overflow-y: auto; /* 如果内容超长允许滚动 */
}

.form-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-fields {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  /* 减小字段间距 */
  width: 100%;
}

.form-field {
  width: 100%;
  margin-bottom: 0;
  /* 移除底部边距，使用gap控制间距 */
}

.input-container {
  position: relative;
  width: 100%;
  overflow: hidden;
}

.input-container::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, #4F46E5, #10B981);
  transition: all 0.3s ease;
}

.input-container:focus-within::after {
  width: 100%;
  left: 0;
}

.input-icon {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: center;
  padding-left: 0.75rem;
  color: rgba(255, 255, 255, 0.5);
}

.text-input {
  width: 100%;
  padding: 0.65rem 0.75rem 0.65rem 2.5rem;
  background-color: rgba(30, 30, 35, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-radius: 4px;
  font-size: 0.875rem;
  color: white;
  transition: all 0.3s;
  outline: none;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.text-input::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

.text-input:focus {
  border-color: rgba(100, 100, 200, 0.4);
  background-color: rgba(35, 35, 40, 0.7);
  box-shadow:
    0 4px 10px rgba(0, 0, 0, 0.15),
    0 0 0 1px rgba(100, 100, 200, 0.3);
}

.toggle-password {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  padding-right: 0.75rem;
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: color 0.2s;
}

.toggle-password:hover {
  color: rgba(255, 255, 255, 0.8);
}

/* 修改错误输入样式，不再那么明显的红色 */
.input-error {
  border-color: rgba(239, 68, 68, 0.5);
  box-shadow: 0 0 0 1px rgba(239, 68, 68, 0.1);
}

.input-error:focus {
  border-color: rgba(79, 70, 229, 0.8);
  box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.3);
}

.terms-container {
  display: flex;
  align-items: flex-start;
  margin-top: 0;
  /* 移除顶部边距 */
}

.terms-checkbox {
  width: 1rem;
  height: 1rem;
  margin-top: 0.15rem;
  margin-right: 0.5rem;
  accent-color: #4F46E5;
}

.terms-text {
  font-size: 0.875rem;
  color: rgba(255, 255, 255, 0.7);
  line-height: 1.4;
}

.terms-link {
  color: rgba(180, 180, 250, 0.7);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s;
}

.terms-link:hover {
  color: rgba(200, 200, 255, 0.9);
  text-decoration: none;
}

.register-button {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  background: linear-gradient(135deg, #4F46E5, #7C3AED);
  color: white;
  font-weight: 500;
  padding: 0.85rem 1rem;
  border: none;
  border-radius: 0.5rem;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  height: 50px;
  box-shadow:
    0 4px 12px rgba(79, 70, 229, 0.3),
    0 0 0 1px rgba(255, 255, 255, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.15);
  position: relative;
  overflow: hidden;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  font-size: 0.9rem;
  animation: neonButtonPulse 6s infinite;
}

.register-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, 
    transparent, 
    rgba(255, 255, 255, 0.2), 
    transparent);
  transition: 0.5s;
}

.register-button:hover {
  transform: translateY(-2px);
  background: linear-gradient(135deg, #5E54F3, #8B4FFF);
  box-shadow:
    0 8px 20px rgba(79, 70, 229, 0.4),
    0 0 0 1px rgba(255, 255, 255, 0.15),
    0 0 15px rgba(79, 70, 229, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
  animation: neonButtonPulse 2s infinite;
}

.register-button:hover::before {
  left: 100%;
}

.register-button:active {
  transform: translateY(-1px);
  box-shadow: 0 3px 10px rgba(79, 70, 229, 0.4);
}

.register-button i {
  font-size: 1rem;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: translateX(0);
  }
  50% {
    transform: translateX(3px);
  }
  100% {
    transform: translateX(0);
  }
}

/* 隐藏社交登录区域，因为已在父组件显示 */
.separator,
.social-login {
  display: none;
}

.error-terms {
  padding: 0.5rem;
  border-radius: 4px;
  background-color: rgba(60, 40, 40, 0.25);
  border: 1px solid rgba(220, 90, 90, 0.2);
}

/* 添加内联错误提示样式 */
.error-message {
  color: #ff6b6b;
  font-size: 0.8rem;
  margin-top: 0.3rem;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding-left: 0.5rem;
  animation: fadeIn 0.3s ease;
}

.input-error {
  border-color: rgba(255, 107, 107, 0.5) !important;
  background-color: rgba(255, 107, 107, 0.05) !important;
}

/* 固定按钮容器 */
.fixed-button-container {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background: rgba(30, 30, 40, 0.5); /* 半透明背景 */
  backdrop-filter: blur(5px); /* 模糊效果 */
  z-index: 10;
}

.form-group input {
  width: 100%;
  padding: 0.85rem 1rem;
  background: rgba(255, 255, 255, 0.07);
  border: none;
  border-radius: 8px;
  color: #fff;
  font-size: 0.95rem;
  transition: all 0.3s;
  border-bottom: 2px solid rgba(128, 90, 213, 0.2);
}

.form-group input:focus {
  background: rgba(255, 255, 255, 0.1);
  box-shadow: 0 0 0 2px rgba(128, 90, 213, 0.3);
  border-bottom: 2px solid rgba(128, 90, 213, 0.8);
  outline: none;
}
</style>
